package com.lf.sp.controller.backend;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver;

import com.lf.sp.common.Const;
import com.lf.sp.common.ResponseCode;
import com.lf.sp.common.ServerResponse;
import com.lf.sp.pojo.User;
import com.lf.sp.service.ICategoryService;
import com.lf.sp.service.IUserService;

@Controller
@RequestMapping("/manage/category/")
public class CategoryManageController {
	
	@Autowired
	private IUserService iUserService;
	
	@Autowired
	private ICategoryService iCategoryService;
	/**
	 * 添加品类
	 * @param session
	 * @param categoryName
	 * @param parentId
	 * @return
	 */
	@RequestMapping(value="add_category.do",method=RequestMethod.POST)
	@ResponseBody
	public ServerResponse addCategory(HttpSession session,String categoryName,@RequestParam(value="parentId",defaultValue = "0") int parentId){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user ==null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "未登录，请登陆");
		}
		//检查是否是管理员
		if(!iUserService.checkAdminRole(user).isSuccess()){
			return ServerResponse.createByErrorMessage("当前用户不是管理员，没有权限");
		}
		return iCategoryService.addCategory(categoryName, parentId);
	}
	
	/**
	 * 更新品类名称
	 * @param session
	 * @param categoryId
	 * @param categoryName
	 * @return
	 */
	@RequestMapping(value="set_category_name.do",method=RequestMethod.POST)
	@ResponseBody
	public ServerResponse setCategoryName(HttpSession session,Integer categoryId,String categoryName){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user == null ){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请登录之后再试");
		}
		//检查是否是管理员
		if(!iUserService.checkAdminRole(user).isSuccess()){
			return ServerResponse.createByErrorMessage("登陆的用户不是管理员，没有权限");
		}
		//返回更新结果
		return iCategoryService.setCategoryName(categoryId, categoryName);
	}
	
	/**
	 * 查询该品类下的子类
	 * @param session
	 * @param categoryId
	 * @return
	 */
	@RequestMapping(value="get_children_parallel_category.do",method=RequestMethod.POST)
	@ResponseBody
	public ServerResponse getChildrenParallelCategory(HttpSession session,@RequestParam(value="categoryId",defaultValue="0")Integer categoryId){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user == null ){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请登录之后再试");
		}
		//检查是否是管理员
		if(!iUserService.checkAdminRole(user).isSuccess()){
			return ServerResponse.createByErrorMessage("登陆的用户不是管理员，没有权限");
		}
		//返回查询结果
		return iCategoryService.getChildrenParallelCategory(categoryId);
	}
	/**
	 * 递归查询品类下左右子品类
	 * @param session
	 * @param categoryId
	 * @return
	 */
	@RequestMapping(value="get_category_and_deep_children_category.do",method=RequestMethod.POST)
	@ResponseBody
	public ServerResponse getCategoryAndDeepChildrenCategory(HttpSession session,@RequestParam(value="categoryId",defaultValue="0")Integer categoryId){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user == null ){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请登录之后再试");
		}
		//检查是否是管理员
		if(!iUserService.checkAdminRole(user).isSuccess()){
			return ServerResponse.createByErrorMessage("登陆的用户不是管理员，没有权限");
		}
		//返回查询结果
		
		return iCategoryService.getCategoryAndDeepChildrenCategory(categoryId);
	}
}
